package org.nf.web.embed.server;

import jakarta.servlet.ServletContext;
import org.nf.web.config.Configure;
import org.nf.web.embed.WebServer;

/**
 * @author wangliang
 * @date 2024/4/8
 */
public abstract class AbstractWebServer implements WebServer {

    @Override
    public void run() {
        Configure configure = initConfigure();
        ServletContext servletContext = initServer(configure);
        cacheConfig(servletContext, configure);
        start();
    }

    /**
     * 根据启动类初始化配置信息
     */
    private Configure initConfigure() {
        return Configure.init();
    }

    /**
     * 将配置类保存到ServletContext
     */
    private void cacheConfig(ServletContext sc, Configure configure) {
        sc.setAttribute(Configure.class.getName(), configure);
    }

    /**
     * 初始化web容器
     * @param configure 配置信息
     * @return ServletContext
     */
    protected abstract ServletContext initServer(Configure configure);

    /**
     * 启动容器
     */
    protected abstract void start();
}
